package com.ttl.bc.ar.reqconfirm.db;

import java.util.Iterator;
import java.util.List;

import hibernate.util.HibernateUtil;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.ttl.bc.ap.stkrequest.BCStkRequestSubBean;
import com.ttl.bc.ar.reqconfirm.bean.BCReqConfirmBean;
import com.ttl.bc.ar.reqconfirm.bean.BCReqConfirmSubBean;
import com.ttl.common.db.ADAO;

public class BCReqConfirmDAO extends ADAO {

	@SuppressWarnings("unchecked")
	public void delete(String hql) {
		Session session = null;

		try {
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			session.beginTransaction();

            org.hibernate.Query q = session.createQuery(hql);
            List resultList = q.list();			

            for(int i=0; i<resultList.size(); i++ ) {
            	BCReqConfirmBean h = (BCReqConfirmBean)resultList.get(i);            	
            	h.setIsCancel(1); 
            	h.setIsConfirm(0);
            	h.setBillStatus(0);     
            	h.setIttl_status("-1");
            	
            	Iterator it = h.getBcReqConfirmSubs().iterator();
            	while(it.hasNext()) {
            		BCReqConfirmSubBean sub = (BCReqConfirmSubBean)it.next();
            		sub.setIttl_status("-1");
            	}
            	
                session.update(h);
            }            
            			
			session.getTransaction().commit();
						
        } catch (HibernateException he) {
            he.printStackTrace();        
		//	return false;
		} finally {
			session.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public boolean restore(String hql) {
		Session session = null;

		try {
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			session.beginTransaction();

            org.hibernate.Query q = session.createQuery(hql);
            List resultList = q.list();			

            for(int i=0; i<resultList.size(); i++ ) {
            	BCReqConfirmBean h = (BCReqConfirmBean)resultList.get(i);            	
            	h.setIsCancel(0); 
            	h.setIsConfirm(0);
            	h.setBillStatus(0);     
            	h.setIttl_status("0");
            	
            	Iterator it = h.getBcReqConfirmSubs().iterator();
            	while(it.hasNext()) {
            		BCReqConfirmSubBean sub = (BCReqConfirmSubBean)it.next();
            		sub.setIttl_status("0");
            	}
            	
                session.update(h);
            }            
            			
			session.getTransaction().commit();
			return true;
        } catch (HibernateException he) {
            he.printStackTrace();        
			return false;
		} finally {
			session.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public boolean update(String s, Object o) {
		BCReqConfirmBean r = (BCReqConfirmBean)o;
		Session session = null;

		try {
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			session.beginTransaction();

            String hql = "FROM BCReqConfirmBean h where h.docNo = '"+s+"'";
            org.hibernate.Query q = session.createQuery(hql);
            List resultList = q.list();			

            for(int i=0; i<resultList.size(); i++){
            	BCReqConfirmBean h = (BCReqConfirmBean)resultList.get(i);
            	
            	r.setCreatorCode(h.getCreatorCode());
            	r.setCreateDateTime(h.getCreateDateTime());
            	
            	Iterator it = r.getBcReqConfirmSubs().iterator();
            	while (it.hasNext()) {
            		BCReqConfirmSubBean rsub = (BCReqConfirmSubBean) it.next();
					rsub.setCreatorCode(r.getCreatorCode());
					rsub.setCreateDateTime(r.getCreateDateTime());
				}
            	
            	
                session.delete(h);
            }            
            
			session.save(r);
			session.getTransaction().commit();
			
			return true;
        } catch (HibernateException he) {
            he.printStackTrace();        
			return false;
		} finally {
			session.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public void updateAfterCreatePurchaseOrder(String hql) {
		List result = null;
		Session session = null;

		try {
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			
			session.beginTransaction();
			
			Query q = session.createQuery(hql);
			result = q.list();
			
			if (result != null) {
				for(int i=0; i<result.size(); i++ ) {
					BCReqConfirmBean reqConfirm = (BCReqConfirmBean)result.get(i);
					reqConfirm.setBillStatus(1);
					reqConfirm.setIsCancel(0);
					session.save(reqConfirm);
				}
			}
			
			
			session.getTransaction().commit();						
		}catch(Exception e) {			
			System.out.println(e.toString());
		}finally {
			session.close();
		}
		
	}

	@SuppressWarnings("unchecked")
	public boolean updateStatus(String poRefNo, String status) {
		Session session = null;

		try {
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			session.beginTransaction();
			
			String hql ="FROM BCReqConfirmBean WHERE docNo ='"+poRefNo+"'";
            org.hibernate.Query q = session.createQuery(hql);
            List resultList = q.list();			

            for(int i=0; i<resultList.size(); i++ ) {
            	BCReqConfirmBean h = (BCReqConfirmBean)resultList.get(i);            	
            	h.setIttl_status(status);
		    	Iterator it = h.getBcReqConfirmSubs().iterator();
		    	
		        while(it.hasNext()) {
		        	BCReqConfirmSubBean sub = (BCReqConfirmSubBean)it.next();
		        	sub.setIttl_status(status);
		        }
		        
                session.update(h);
            }            
            			
			session.getTransaction().commit();
			return true;
        } catch (HibernateException he) {
            he.printStackTrace();        
			return false;
		} finally {
			session.close();
		}
		
	}

	@SuppressWarnings("unchecked")
	public boolean updateStatus(String docNo, String[] rowOrderLineItem, String status) {
		Session session = null;

		try {
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			session.beginTransaction();
			
			String hql ="FROM BCReqConfirmBean WHERE docNo ='"+docNo+"'";
            org.hibernate.Query q = session.createQuery(hql);
            List resultList = q.list();			

            for(int i=0; i<resultList.size(); i++ ) {
            	BCReqConfirmBean h = (BCReqConfirmBean)resultList.get(i);            	
            	
		    	Iterator it = h.getBcReqConfirmSubs().iterator();		    	
		        while(it.hasNext()) {
		        	BCReqConfirmSubBean sub = (BCReqConfirmSubBean)it.next();
		        	
		        	for (int j = 0; j < rowOrderLineItem.length; j++) {
						if(rowOrderLineItem[j]!=null && sub.getRowOrder()==Integer.valueOf(rowOrderLineItem[j])) {
							sub.setIttl_status(status);
						}
					}		        	
		        }	
		        
                session.update(h);
            }            
            			
			session.getTransaction().commit();
			return true;
        } catch (HibernateException he) {
            he.printStackTrace();        
			return false;
		} finally {
			session.close();
		}
		
	}

}
